var API = API || {};

API.x_sorter = {
    SorterManager: function (sort_by_container) {
        var sort_by_container = sort_by_container;

        this.sort = function (myItems) {



            if ($('.sort-link').length == 0) {
                var sortBy = $(sort_by_container).val();
            } else {
                var sortBy = $('.sort-link.active').attr('data-id');
            }

            switch (sortBy) {

                case 'price':

                    myItems.sort(function (a, b) {
                        return a.sort_price - b.sort_price;
                    });

                    break;

                case 'recommended':

                    myItems.sort(function (a, b) {
                        return a.object_search_priority - b.object_search_priority;
                    });

                    break;

                case 'number_of_stars':
                    myItems.sort(function (b, a) {
                        return a.object_number_of_stars - b.object_number_of_stars;
                    });

                    break;

                case 'object_name':
                    myItems.sort(function (a, b) {
                        var x = a.object_name.substr(0, 1).toLowerCase();
                        var y = b.object_name.substr(0, 1).toLowerCase();
                        return x < y ? -1 : x > y ? 1 : 0;
                    });

                    break;

            }
        };
    }
};
